// Java VM for small microcontrollers
//
// (c) 2012, Digital Six Laboratories LLC
// All Rights Reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Original work based on NanoVM (http://www.harbaum.org/till/nanovm/index.shtml) for AVR microcontroller
//=========================================================================================================
// 
// uart.c
//
// Si1000 platform UART api

#include "platform_types.h"
#include "uart.h"


// Circular buffer for UART byte reception
U8 xdata rxBuffer[BUFFER_SIZE];
// Circular buffer for UART byte transmission
U8 xdata txBuffer[BUFFER_SIZE];

// Head pointer for receive buffer
U8 xdata rxHead;
// Tail pointer for receive buffer
U8 xdata rxTail;
// Head pointer for transmit buffer
U8 xdata txHead;
// Tail pointer for transmit buffer
U8 xdata txTail;


// ** void UartSendByte(U8 b2s) **
//
// Stuff a byte in the transmit UART buffer and advance the head pointer.  If the head and tail
// pointer are equal, that means that the UART interrupts are not working because the buffer
// was empty.  In that case, we need to force the interrupt to happen so that the first byte will
// be sent.

void UartSendByte(U8 b2s)
{
	U8 kickStart;
	kickStart=0;
	if(txHead == txTail)
		kickStart=1;
	txBuffer[txHead++] = b2s;
	txHead &= BUFFER_MASK;
	// force an interrupt if the buffer was empty
	if(kickStart>0)
		TI0=1;
}

// ** U16 UartBytesAvailable(void) **
//
// Returns the number of bytes available in the receiver buffer.

U16 UartBytesAvailable(void)
{
	U16 retVal;

	retVal = rxTail - rxHead;
	retVal &= BUFFER_MASK;
	
	return retVal;
}

// ** U8 UartReadByte(void) **
//
// Reads a byte, if available, from the receive buffer.  If a byte is not available, this function
// will return a 0.

U8 UartReadByte(void)
{
	U8 retVal=0;
	if(rxHead != rxTail)
		retVal = rxBuffer[rxTail++];
	rxTail &= BUFFER_MASK;

	return retVal;
}

// ** void UartFlush(void) **
//
// Empty both buffers by setting both head and tail pointers to the start of the buffer.

void UartFlush(void)
{	
	rxHead = 0;
	rxTail = 0;
	txHead = 0;
	txTail = 0;
}

// ** U8 InitializeUart(UartBaudRates baudRate)
//
// Call this function to set the UART baud rate.  The possible baud rates are 
// defined in the UartBaudRates enumeration.

U8 InitializeUart( UartBaudRates baudRate )
{

	U8 retVal;

	retVal=1;

	rxHead = 0;
	rxTail = 0;
	txHead = 0;
	txTail = 0;

    TMOD    &= ~0xF0;
    TMOD    |= 0x20;
    TR1 = 1;
    TI0 = 1;
    ES0 = 1;
    SCON0   = 0x10;
    CKCON   &= ~0x0b;

	switch(baudRate)
	{
		case b1200:
			CKCON |= 0x02;
			TH1 = 0x2b;
			TL1 = TH1;
			break;
		case b2400:
			CKCON |= 0x02;
			TH1 = 0x96;
			TL1 = TH1;
			break;
		case b9600:
			CKCON |= 0x00;
			TH1 = 0x96;
			TL1 = TH1;
			break;
		case b14400:
			CKCON |= 0x00;
			TH1 = 0xb9;
			TL1 = TH1;
			break;
		case b28800:
			CKCON |= 0x01;
			TH1 = 0x96;
			TL1 = TH1;
			break;
		case b57600:
			CKCON |= 0x08;
			TH1 = 0x2b;
			TL1 = TH1;
			break;
		case b115200:
			CKCON |= 0x08;
			TH1 = 0x96;
			TL1 = TH1;
			break;
		case b230400:
			CKCON |= 0x08;
			TH1 = 0xCB;
			TL1 = TH1;

			break;
		default:
			retVal=0;
			break;
	}


	return retVal;
}

// *** UART Interrupt Handler ***
//
// Processes transmit and receive byte interrupts from the UART, putting received bytes in a
// circular RX buffer and sending bytes using a circular TX buffer.

INTERRUPT( UART0_ISR, INTERRUPT_UART0 )
{
    if ( RI0 == 1 ) 
	{
		// Clear it
        RI0 = 0;
		// rxHead is the pointer we use to keep track of where we are in the
		// receiver buffer.  rxTail is used by the API to take characters out 
		// of the buffer.
		if(rxHead!=rxTail)
			rxBuffer[rxHead++] = SBUF0;
		// wrap the pointer to make the buffer circular
		rxHead&=BUFFER_MASK;
    }
    if ( TI0 == 1 ) 
	{
		// Clear it.
        TI0 = 0;
        
		// txTail is the pointer we use to keep track of where we are 
		// in the transmitter buffer.  txHead is used by the API to put
		// characters in the buffer
		if(txTail!=txHead)
			SBUF0 = txBuffer[txTail++];
		// wrap the pointer to make the buffer circular
		txTail&=BUFFER_MASK;
        
    }
}
